﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Reborn_Server.App1AppHandler;
using App1AppHandler;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_FreezingAura : Buff
    {
        public Spell_FreezingAura()
        {
            minutesLeft = 3;
        }

        public override bool castWhileAlone(SpellIcon sIcon)
        {
            return true;
        }

        public override void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            minutesLeft = timeRemaining;
            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);

            spellTimer.Start();
        }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            if (spellIcon.Name.IndexOf("Rank 2") > -1)
                minutesLeft = CastingCreatureWis / 8;

            TargetCreatureBase.buffManager.UpdateBuffs();

            foreach (MapItem mapitem in MapItemsWithin12)
            {
                if (Math.Abs(mapitem.MapX - myCreatureBase.MapX) <= 8 && Math.Abs(mapitem.MapY - myCreatureBase.MapY) <= 8)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                        {
                            Character character = (Character)creaturebase;
                            character.PlaySound("BuffCast", character.MapX, character.MapY);
                        }
                    }
                }
            }

            //Start Spell Timer
            spellTimer.Start();
        }

        //Try freeze calculates the chance of proc based on wisdom, if proc is successfull
        //The creature will still have a chance to resist.
        public void tryFreeze(CreatureBase attackingCreatureBase)
        {
            int wisdomMod = SkillManager.AdjustForCombat(CastingCreatureLevel, 0, CastingCreatureWis);
            int range = wisdomMod + 65;

            int result = Managers.GameHandler.random.Next(1, range + 1);

            //Unsuccessfull roll, proc will not attempt this time
            if (result > wisdomMod)
                return;

            //Proc roll successfull, allow attack to try resist
            Spell_Mesmerize freezeBuff = new Spell_Mesmerize();
            
            freezeBuff.spellIcon = this.spellIcon.clone();
            freezeBuff.spellIcon.Offensive = true;
            
            //Do NOT save the offensive version. Player will log in and have the defensive version instead.
            freezeBuff.spellIcon.ID_DB = -1;

            if (freezeBuff.tryResist(freezeBuff.spellIcon, CastingCreatureLevel, CastingCreatureInt, CastingCreatureWis, attackingCreatureBase) == true)
            {
                if (attackingCreatureBase != TargetCreatureBase)
                    TargetCreatureBase.SendMessage(attackingCreatureBase.Name + " resists " + spellIcon.Name + "!", "spelldefense");

                attackingCreatureBase.SendMessage("You resist " + TargetCreatureBase.Name + "'s " + spellIcon.Name + "!", "spelloffense");
                return;
            }

            attackingCreatureBase.SendMessage("You are frozen by " + TargetCreatureBase.Name + "'s " + spellIcon.Name + "!", "spelloffense");
            TargetCreatureBase.SendMessage("Your " + spellIcon.Name + " freezes " + attackingCreatureBase.Name + "!", "spelloffense");

            int roundsToFreeze = 3;
            if (spellIcon.Name.IndexOf("Rank 2") > -1)
                roundsToFreeze = 5;

            freezeBuff.Resume(attackingCreatureBase, freezeBuff.spellIcon, roundsToFreeze, CastingCreatureLevel,
                CastingCharacterID, CastingCreatureInt, CastingCreatureWis, -1, null);

            attackingCreatureBase.buffManager.UpdateBuffs();
        }
    }
}
